﻿@using FTBS.Models.BLO
@model PagedList.IPagedList<FTBS.Models.Entities.Concert.ScheduleConcertEntity>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

@{
    ViewBag.Title = "Quản lý vé";
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
    ViewBag.Count = 0;
    TicketBLO ticketBLO = new TicketBLO();
    var isSortDes = (bool)ViewBag.IsSortDes;

    var fromDate = (DateTime)ViewBag.FromDate;
    var toDate = (DateTime)ViewBag.ToDate;
}

<legend style="margin-top: 10px">QUẢN LÝ VÉ</legend>
<h6 style="font-weight: bold; color: red; font-size: 14px">Chọn suất chiếu để tạo hoặc thay đổi vé!</h6>

<div>
    <div>
        @using (Html.BeginForm("Index", "Ticket", FormMethod.Get))
        {
            <table class="table striped bordered hovered">
                <tr>
                    <th>Tìm kiếm:
                    </th>
                    <td>
                        @Html.TextBox("SearchString", ViewBag.CurrentFilter as string, new { tyle = "width: 200px; height: 50px;" })
                    </td>
                    <th>Từ ngày:
                    </th>
                    <td>
                        <div class="input-control text" data-role="datepicker"
                             data-date='@fromDate.ToShortDateString()'
                             data-format="dd/mm/yyyy"
                             data-position="bottom"
                             data-effect="slide"
                             data-other-days='1'>
                            <input type="text" id="FromDate" name="FromDate">
                            <button class="btn-date" type="button"></button>
                        </div>
                    </td>
                    <th>Đến ngày:
                    </th>
                    <td>
                        <div class="input-control text" data-role="datepicker"
                             data-date='@toDate.ToShortDateString()'
                             data-format="dd/mm/yyyy"
                             data-position="bottom"
                             data-effect="slide"
                             data-other-days='1'>
                            <input type="text" id="ToDate" name="ToDate">
                            <button class="btn-date" type="button"></button>
                        </div>
                    </td>
                    <td>
                        <input class="inverse" type="submit" value="Tìm" style="height: 35px" />
                    </td>
                </tr>
            </table>
        }
    </div>

    <div style="height: 330px; width: 100%">
        <table class="table striped bordered hovered">
            <tr>
                <th>
                    @Html.ActionLink("Phim", "Index", new { sortOrder = ViewBag.NameSortParm, isSortDescending = isSortDes })
                </th>
                <th>
                    @Html.ActionLink("Rạp", "Index", new { sortOrder = "theater_desc", isSortDescending = isSortDes })
                </th>
                <th>
                    @Html.ActionLink("Phòng", "Index", new { sortOrder = "room_desc", isSortDescending = isSortDes })
                </th>
                <th>
                    @Html.ActionLink("Giờ chiếu", "Index", new { sortOrder = "time_desc", isSortDescending = isSortDes })
                </th>
                <th>
                    @Html.ActionLink("Ngày chiếu", "Index", new { sortOrder = "day_desc", isSortDescending = isSortDes })
                </th>
                <th></th>
                <th></th>
            </tr>
            @foreach (var item in Model)
            {
                if (!ticketBLO.CheckScheduleHasTickets(item.ScheduleId))
                {
                    <tr>
                        <td style="text-align: left; width: 200px">
                            @Html.DisplayFor(modelItem => item.FilmName)
                        </td>
                        <td style="text-align: left; width: 200px">
                            @Html.DisplayFor(modelItem => item.TheaterName)
                        </td>
                        <td style="text-align: center">
                            @Html.DisplayFor(modelItem => item.RoomName)
                        </td>
                        <td style="text-align: center">
                            @Html.DisplayFor(modelItem => item.TimeForShow)
                        </td>
                        <td style="text-align: center">
                            @{
                                var dateOfSchedule = item.Date.ToString("dd/MM/yyyy");
                            }
                            @dateOfSchedule
                        </td>
                        @if (item.IsActive == false)
                        {
                            <td>Chưa khởi chiếu
                            </td>
                        }
                        else
                        {
                            <td>Đang khởi chiếu
                            </td>
                        }
                        <td>
                            <button type="submit" onclick="createTicket(@item.ScheduleId)" style="background: none; border: none; display: inline; font: inherit; margin: 0; padding: 0; outline: none; outline-offset: 0; color: #2e92cf; cursor: pointer; text-decoration: underline;">
                                Tạo vé</button>
                            @*@Html.ActionLink("", "CreateTickets", new { scheduleId = item.ScheduleId })*@
                        </td>

                    </tr>
                }
                else if (ticketBLO.CheckScheduleHasTickets(item.ScheduleId))
                {
                    <tr style="color: red">
                        <td style="text-align: left; width: 200px">
                            @Html.DisplayFor(modelItem => item.FilmName)
                        </td>
                        <td style="text-align: left; width: 200px">
                            @Html.DisplayFor(modelItem => item.TheaterName)
                        </td>
                        <td style="text-align: center">
                            @Html.DisplayFor(modelItem => item.RoomName)
                        </td>
                        <td style="text-align: center">
                            @Html.DisplayFor(modelItem => item.TimeForShow)
                        </td>
                        <td style="text-align: center">
                            @{
                                var dateOfSchedule = item.Date.ToString("dd/MM/yyyy");
                            }
                            @dateOfSchedule
                        </td>
                        @if (item.IsActive == false)
                        {
                            <td>Chưa khởi chiếu
                            </td>
                        }
                        else
                        {
                            <td>Đang khởi chiếu
                            </td>
                        }
                        <td>
                            <button type="submit" onclick="createTicket(@item.ScheduleId)" style="background: none; border: none; display: inline; font: inherit; margin: 0; padding: 0; outline: none; outline-offset: 0; color: #2e92cf; cursor: pointer; text-decoration: underline;">
                                Sửa vé</button>
                            @*@Html.ActionLink("", "CreateTickets", new { scheduleId = item.ScheduleId })*@
                        </td>

                    </tr>
                }
            }
        </table>

    </div>

    <div style="float: right">
        <br />
        <div style="float: right">
            Trang @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) của @Model.PageCount trang
        </div>
        <div>
            @Html.PagedListPager(Model, page => Url.Action("Index",
                                                           new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter, fromDate = fromDate.ToString("dd/MM/yyyy"), toDate = toDate.ToString("dd/MM/yyyy") }))
        </div>
    </div>
</div>
<div id="wrapper">
    <div style="display: none;" id="overlay"></div>
    <div style="display: none;" id="popup">
        <img src="~/Image/Icon/loading.gif" />
        <div style="font-weight: bold">Đang xử lý thao tác ...</div>
    </div>
</div>
<style>
    body{padding:0; margin:0; font-family:arial;}
	#wrapper{
		width:600px;
		margin:10px auto;
		text-align:center;
	}

	#overlay {
		background: #000000;
		opacity:0.5;
		bottom: 0;
		left: 0;
		position: fixed;
		right: 0;
		top: 0;
		z-index: 10000;
	}
	
	#popup {
		background: none repeat scroll 0 0 #FFFFFF;
		border: 20px solid #DDDDDD;
		left: 37.5%;
		padding: 50px;
		position: fixed;
		text-align: center;
		top: 28%;
		width: 280px;
		z-index: 20000;
		-moz-border-radius:30px 0;
	}
	</style>

<script>
    function createTicket(scheduleId) {
        $.ajax({
            url: '/Ticket/CheckPriceTicketToCreateTicket',
            type: 'POST',
            data: { scheduleId: scheduleId },
            success: function (response) {
                if (response == "SessionTimeOut") {
                    window.location.href = "/Home/Announcement";
                }
                if ((response == "not enough chairtype") || (response == "fail")) {
                    alert('Hãy tạo đầy đủ bảng giá của các loại ghế cho rạp!');
                }
                if (response == "price less zero") {
                    alert('Giá vé mỗi loại ghế của rạp không thấp hơn 0 VNĐ!');
                }
                if (response == "can create ticket") {
                    jQuery("#overlay").css('display', 'block'); // displaying the overlay
                    jQuery("#popup").css('display', 'block'); // displaying the popup
                    window.location.href = "/Ticket/CreateTickets?scheduleId=" + scheduleId;
                }
            }
        });
    }
</script>

